package com.xls.commonlibrary.http.biz;

import android.annotation.SuppressLint;
import android.webkit.ValueCallback;

import androidx.appcompat.app.AppCompatActivity;

import com.orhanobut.logger.Logger;
import com.xls.commonlibrary.http.Api;
import com.xls.commonlibrary.http.RetrofitHelper;
import com.xls.commonlibrary.http.RxLifecycle;
import com.xls.commonlibrary.http.Transformer;
import com.xls.commonlibrary.util.LoginUtil;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;

public class UserUtil {
    private static volatile UserUtil singleton = null;

    private UserUtil() {
    }

    public static UserUtil getInstance() {
        if (singleton == null) {
            synchronized (UserUtil.class) {
                if (singleton == null) {
                    singleton = new UserUtil();
                }
            }
        }
        return singleton;
    }


    ////////////////////////////////////////////////////

    @SuppressLint("CheckResult")
    public void getUserGameAccount(
            @Nonnull AppCompatActivity activity,
            @Nonnull ValueCallback<Boolean> callback
    ) {
        Map<String, String> data = new HashMap<>();

        RetrofitHelper.getInstance()
                .create(Api.class)
                .getUserGameAccount(data)
                .compose(Transformer.transform())
                .compose(RxLifecycle.bindRxLifecycle(activity))
                .subscribe(
                        result -> {
                            Logger.d("=======getUserGameAccount=========:" + result.getData().toString());
                            LoginUtil.getInstance().saveUser(result.getData());

                            if (callback != null) {
                                callback.onReceiveValue(true);
                            }


                        },
                        throwable -> {
                            if (callback != null) {
                                callback.onReceiveValue(false);
                            }
                            Logger.d(throwable);
                        }
                );

    }

    @SuppressLint("CheckResult")
    public void updateUserGameAccount(
            @Nonnull AppCompatActivity activity,
            @Nonnull String level,
            @Nonnull ValueCallback<Boolean> callback
    ) {

        Map<String, String> data = new HashMap<>();
        data.put("level", level);

        RetrofitHelper.getInstance()
                .create(Api.class)
                .updateUserGameAccount(data)
                .compose(Transformer.transform())
                .compose(RxLifecycle.bindRxLifecycle(activity))
                .subscribe(
                        result -> {
                            Logger.d("=======updateUserGameAccount=========:" + result.getData().toString());
                            LoginUtil.getInstance().saveUser(result.getData());

                            if (callback != null) {
                                callback.onReceiveValue(true);
                            }
                        },
                        throwable -> {
                            if (callback != null) {
                                callback.onReceiveValue(false);
                            }
                            Logger.d(throwable);
                        }
                );

    }
}
